﻿function Flow(SelfObjName, TabRadioID, ListID, ListTDID, PageTRID, SearchTRID, TitleTRID, Tabs) {
    this.selfObjName = SelfObjName;
    this.tabs = Tabs;
    this.listID = ListID;
    this.ListTDID = ListTDID;
    this.pageTRID = PageTRID;
    this.searchTRID = SearchTRID;
    //常规组件    
    window.progressDialog = new ProgressDialog();
    window.infoDialog = new InfoDialog();
    window.ajaxClass = new AjaxClass();
    //设置表格自动尺寸    
    window.resizeFlexiGrid = new ResizeFlexiGrid(".flexigrid", "#" + ListID, ".flexigrid .bDiv", ["#" + TitleTRID, "#" + SearchTRID, "#" + PageTRID, ".flexigrid .hDiv"], 4, 4);
    //获得初始选项夹
    var TabIndex = null;
    for (var i = Tabs.length - 1; i >= 0; i--) {
        if (HasPermission(Tabs[i].Permission)) {
            TabIndex = i;
            if (JsonOriginal.GetList["Status"] == Tabs[i].Status) {
                break;
            }
        }
    }
    //添加选项夹  
    for (var i = 0; i < Tabs.length; i++) {
        $("#" + TabRadioID).append("<input type=\"radio\" id=\"radio" + i + "\" name=\"TabRadio\"" + (HasPermission(Tabs[i].Permission) ? "" : "disabled=\"disabled\"") + " value=\"" + i + "\" " + (TabIndex == i ? "checked=\"checked\"" : "") + " /><label for=\"radio" + i + "\">" + Tabs[i].TabName + "</label>");
    }
    $("#" + TabRadioID).buttonset();
    eval("$(\"#\" + TabRadioID + \" input[name='TabRadio']\").change(function () { window." + this.selfObjName + ".LoadGridData(parseInt(this.value)); });");
    //添加添加按钮 
    for (var i = 0; i < Tabs.length; i++) {
        if (Tabs[i].Buttons != null) {
            for (var j = 0; j < Tabs[i].Buttons.length; j++) {
                $("#" + SearchTRID + " .Search").append("<button id=\"" + Tabs[i].Buttons[j].ID + "\" style=\"margin-left:0px;margin-right:" + (j == Tabs[i].Buttons.length - 1 ? "16" : "4") + "px;\">" + Tabs[i].Buttons[j].ButtonName + "</button>");
                if (Tabs[i].Buttons[j].Icon != null) {
                    $("#" + Tabs[i].Buttons[j].ID).button({ icons: Tabs[i].Buttons[j].Icon });
                } else {
                    $("#" + Tabs[i].Buttons[j].ID).button();
                }
                if (HasPermission(Tabs[i].Buttons[j].Permission)) {
                    $("#" + Tabs[i].Buttons[j].ID).click(Tabs[i].Buttons[j].Click);
                } else {
                    $("#" + Tabs[i].Buttons[j].ID).button("option", "disabled", true);
                }
            } 
        }
    }        
    //添加时间选择 
    $("#" + SearchTRID + " .Search").append("<span class=\"SearchSplit\" style=\"margin-left:0px;\">日期</span><form id=\"SDate\"><input type=\"text\" name=\"@SDate\" style=\"width: 70px;\"></form><span class=\"SearchSplit\">～</span><form id=\"EDate\"><input type=\"text\" name=\"@EDate\" style=\"width: 70px;margin-right: 16px;\"></form><span class=\"SearchSplit\">关键字</span>");
    $("#SDate").css("display", "none");
    $("#EDate").css("display", "none");
    $("#" + SearchTRID + " .Search .SearchSplit").css("display", "none");
    $("#" + SearchTRID + " .Search input[name='@SDate']").datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        changeYear: true,
        defaultDate: 0,
        onClose: function (selectedDate) {
            $("#" + SearchTRID + " .Search input[name='@EDate']").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#" + SearchTRID + " .Search input[name='@EDate']").datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        changeYear: true,
        defaultDate: 0,
        onClose: function (selectedDate) {
            $("#" + SearchTRID + " .Search input[name='@SDate']").datepicker("option", "maxDate", selectedDate);
        }
    });
    $("#SDate,#EDate").submit(function () {
        window.flexiGrid.searchKey = $("#" + ListID + "_SearchKey").val();
        window.flexiGrid.PageNum = 1;
        window.flexiGrid.LoadListData();
        return false;
    });
    $("#SDate input").blur(function () { if ($(this).val() == "") { $(this).val("0001-01-01"); } });
    $("#EDate input").blur(function () { if ($(this).val() == "") { $(this).val("9999-12-31"); } });

    //首次加载
    if (TabIndex != null) {
        setTimeout("window." + this.selfObjName + ".LoadGridData(" + TabIndex + ");", 0);
    }         
    //加载表格
    this.LoadGridData = function (TabIndex) {
        //删除原有内容
        $("#ListTD").html("");
        $("#SearchTR .Search button[id$='_SearchButton'], #SearchTR .Search form[id$='_SearchKeyForm']").remove();
        $("#PageTR .Page").html("");
        //按钮时间
        for (var i = 0; i < this.tabs.length; i++) {
            //设置时间
            if (i == TabIndex) {
                if (this.tabs[i].DateFilter) {
                    var date = new Date();
                    if (JsonOriginal.GetList["SDate"] != null && JsonOriginal.GetList["SDate"] != undefined && JsonOriginal.GetList["SDate"] != "") {
                        $("#SearchTR .Search input[name='@SDate']").val(JsonOriginal.GetList["SDate"]);
                    } else if (this.tabs[i].DateDefult == "Today") {
                        $("#SearchTR .Search input[name='@SDate']").val(date.pattern("yyyy-MM-dd"));
                    } else if (this.tabs[i].DateDefult != null && this.tabs[i].DateDefult != undefined) {
                        $("#SearchTR .Search input[name='@SDate']").val(this.tabs[i].DateDefult);
                    } else {
                        $("#SearchTR .Search input[name='@SDate']").val("");
                    }
                    if (JsonOriginal.GetList["EDate"] != null && JsonOriginal.GetList["EDate"] != undefined && JsonOriginal.GetList["EDate"] != "") {
                        $("#SearchTR .Search input[name='@EDate']").val(JsonOriginal.GetList["EDate"]);
                    } else if (this.tabs[i].DateDefult == "Today") {
                        $("#SearchTR .Search input[name='@EDate']").val(date.pattern("yyyy-MM-dd"));
                    } else if (this.tabs[i].DateDefult != null && this.tabs[i].DateDefult != undefined) {
                        $("#SearchTR .Search input[name='@EDate']").val(this.tabs[i].DateDefult);
                    } else {
                        $("#SearchTR .Search input[name='@EDate']").val("");
                    }
                    $("#SDate").css("display", "inline");
                    $("#EDate").css("display", "inline");
                    $(".SearchSplit").css("display", "inline");
                } else {
                    $("#SearchTR .Search input[name='@SDate']").val("");
                    $("#SearchTR .Search input[name='@EDate']").val("");
                    $("#SDate").css("display", "none");
                    $("#EDate").css("display", "none");
                    $(".SearchSplit").css("display", "none");
                }
            }
            //显示按钮
            if (this.tabs[i].Buttons != null) {
                for (var j = 0; j < this.tabs[i].Buttons.length; j++) {
                    $("#" + this.tabs[i].Buttons[j].ID).css("display", (i == TabIndex) ? "inline" : "none");
                }
            }
        }
        //添加表格
        window.flexiGrid = new FlexiGrid("flexiGrid", this.listID, this.ListTDID, this.pageTRID, this.searchTRID, "resizeFlexiGrid", this.tabs[TabIndex].ColModel, null, JsonOriginal.GetList["SortName"], JsonOriginal.GetList["SortOrder"], JsonOriginal.GetList["SearchKey"], false, null, null);
        window.resizeFlexiGrid.Resize("resizeFlexiGrid");
        window.flexiGrid.listName = this.tabs[TabIndex].ListName;
        //读取数据
        if (this.tabs[TabIndex].StatisticsName != undefined) {
            $('#' + window.flexiGrid.searchTRID + ' .Search .AmountTitle, #' + window.flexiGrid.searchTRID + ' .Search .ConfirmAmount').remove();
            window.flexiGrid.LoadData("", "$('#" + window.flexiGrid.searchTRID + " .Search').append('<span class=\"AmountTitle\" style=\"margin-left:12px;vertical-align: -1px;\">" + this.tabs[TabIndex].StatisticsName + "：</span><span class=\"ConfirmAmount\" style=\"vertical-align: -1px;\">' + data.ReturnData[1] + '</span>');", "", null, [[{ "OperationKey": this.tabs[TabIndex].ListName + "统计", "Parameters": window.flexiGrid.GetParameters()}]]);
            $("#SDate, #EDate, #" + window.flexiGrid.listID + "_SearchButton, #" + window.flexiGrid.listID + "_SearchKeyForm").unbind();
            eval("$(\"#" + window.flexiGrid.listID + "_SearchButton\").click(function () { window[\"" + window.flexiGrid.selfObjName + "\"].searchKey = $(\"#" + window.flexiGrid.listID + "_SearchKey\").val(); window['" + window.flexiGrid.selfObjName + "'].PageNum = 1; window[\"" + window.flexiGrid.selfObjName + "\"].LoadData('', \"$('#" + window.flexiGrid.searchTRID + " .Search .ConfirmAmount').text(data.ReturnData[1]);\", '', null, [[{ 'OperationKey': '" + this.tabs[TabIndex].ListName + "统计" + "', 'Parameters': window['" + window.flexiGrid.selfObjName + "'].GetParameters()}]]); });");
            eval("$(\"#SDate, #EDate, #" + window.flexiGrid.listID + "_SearchKeyForm\").submit(function () { window[\"" + window.flexiGrid.selfObjName + "\"].searchKey = $(\"#" + window.flexiGrid.listID + "_SearchKey\").val(); window['" + window.flexiGrid.selfObjName + "'].PageNum = 1; window[\"" + window.flexiGrid.selfObjName + "\"].LoadData('', \"$('#" + window.flexiGrid.searchTRID + " .Search .ConfirmAmount').text(data.ReturnData[1]);\", '', null, [[{ 'OperationKey': '" + this.tabs[TabIndex].ListName + "统计" + "', 'Parameters': window['" + window.flexiGrid.selfObjName + "'].GetParameters()}]]); return false; });");
        } else {
            $('#' + window.flexiGrid.searchTRID + ' .Search .AmountTitle, #' + window.flexiGrid.searchTRID + ' .Search .ConfirmAmount').remove();
            window.flexiGrid.LoadData("", "", "", null, null);
        }
    }
}